package pl.koszolko.documentclustering.resourceprovider.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import pl.koszolko.documentclustering.resourceprovider.HTMLResourceStrategy;

public class ACMRegExpResourceStrategy implements HTMLResourceStrategy{
	private static String AUTHORS = "(((Author Profile Page)|(Search for (.)+)){1}\" target=\"_self\">(\\w|\\s|[\\.-])+(</a>){0})";
	private static String ABSTRACT = "ABSTRACT</A></h1>(.)*<div style=\"display:inline\">((.)*)(</div>){0}";
	private static String TITLE = "(<h1 class=\"mediumb-text\" style=\"margin-top:0px; margin-bottom:0px;\"><strong>(.)*(</strong></h1>){0})";
	private static String REFERENCES = "<div class=\"abstract\">\\s*(\\[?\\d*\\]?)\\s*</div>\\s*</td>\\s*<td>\\s*<div>\\s*<a href=\"(\\w|[?.&=])*";
	private static String CITEDBY = "<td valign=\"top\">\\s*(&nbsp;|<img src=\"http://dl.acm.org/images/ACM_mini.jpg\" width=\"25\" height=\"24\" alt=\"\" vspace=\"0\" border=\"0\" align=\"top\">)\\s*</td>\\s*<td>\\s*<div>\\s*<a href=\"(\\w|[?.&=])*";
	private String htmlPage;
	
	public ACMRegExpResourceStrategy(String htmlPage) {
		//taki ugly trick aby wycinac niepotrzebne informacje (czesc dokumentu html),
		//ktore moga wprowadzac bedy
		this.htmlPage = htmlPage.split("<SPAN class=\"heading\">PUBLICATION</SPAN>")[0];
	}
	
	@Override
	public List<String> getAuthors() {
		Set<String> authors = new TreeSet<String>();
		
		Pattern pattern = Pattern.compile(AUTHORS);
		Matcher matcher = pattern.matcher(htmlPage);

		while(matcher.find()) {
			String author = matcher.group();
			authors.add(getAuthorFromRegex(author));
		}
		
		return new ArrayList<String>(authors);
	}

	private String getAuthorFromRegex(String author) {
		String tmp[] = author.split(">");
		return tmp[1];
	}

	@Override
	public String getDocumentName() {
		String title = null;
		
		Pattern pattern = Pattern.compile(TITLE);
		Matcher matcher = pattern.matcher(htmlPage);

		if(matcher.find()) {
			title = getTitleFromRegex(matcher.group());
			
		}
		
		return title;
	}

	private String getTitleFromRegex(String group) {
		String tmp[] = group.split("((<)|(</))strong>");
		if(tmp.length > 1)
			return tmp[1];
		
		return null;
	}

	@Override
	public String getAbstract() {
		String abstractText = null;
		
		Pattern pattern = Pattern.compile(ABSTRACT, Pattern.DOTALL);
		Matcher matcher = pattern.matcher(htmlPage);

		if(matcher.find()) {
			abstractText = matcher.group();
		}
		
		return getAbstractFromRegex(abstractText);
	}

	private String getAbstractFromRegex(String abstractText) {
		if(abstractText == null)
			return null;
		
		String tmp[] = abstractText.split("<div style=\"display:inline\">");
		if(tmp.length > 1) {
			return tmp[1].split("</div>")[0].replaceAll("[</]{1,3}p>", "");
		}
		else
			return null;
	}

	@Override
	public List<String> getReferences() {
		List<String> references = new ArrayList<String>();
		
		Pattern pattern = Pattern.compile(REFERENCES);
		Matcher matcher = pattern.matcher(htmlPage);

		while(matcher.find()) {
			String ref = getRefFromRegex(matcher.group());
			if(ref != null)
				references.add("http://dl.acm.org/" + ref + "&preflayout=flat");
		}
		
		return references;
	}

	private String getRefFromRegex(String ref) {
		String tmp[] = ref.split("<a href=\"");
		if(tmp.length > 1)
			return tmp[1];
		return null;
	}

	@Override
	public List<String> getSearchLinks(String responseString) {
		return null;
	}
	
	public Set<String> getCitedBy() {
		Set<String> citedBy = new TreeSet<String>();
		
		Pattern pattern = Pattern.compile(CITEDBY);
		Matcher matcher = pattern.matcher(htmlPage);

		while(matcher.find()) {
			String link = getCitedByFromRegex(matcher.group());
			if(link != null)
				citedBy.add("http://dl.acm.org/" + link + "&preflayout=flat");
		}
		
		return citedBy;
	}

	private String getCitedByFromRegex(String group) {
		String tmp[] = group.split("<a href=\"");
		if(tmp.length > 1)
			return tmp[1];
		return null;
	}

}
